package com.railwayservice.serviceprovider.web;

import com.railwayservice.application.cache.ServiceProviderSessionCache;
import com.railwayservice.application.config.AppConfig;
import com.railwayservice.application.vo.PageData;
import com.railwayservice.application.vo.PageParam;
import com.railwayservice.application.vo.ResultMessage;
import com.railwayservice.serviceprovider.entity.ServiceProvider;
import com.railwayservice.serviceprovider.service.DeliverService;
import com.railwayservice.serviceprovider.service.ServiceProviderService;
import com.railwayservice.serviceprovider.vo.CountOrdersVO;
import com.railwayservice.serviceprovider.vo.DeliverOrderVO;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 送货模块请求控制器。
 *
 * @author Ewing
 */
@Controller
@RequestMapping(value = "/deliver", method = {RequestMethod.GET, RequestMethod.POST})
@Api(value = "送货模块请求控制器", description = "送货模块的相关操作")
public class DeliverController {
    private final Logger logger = LoggerFactory.getLogger(DeliverController.class);

    private ServiceProviderSessionCache serviceProviderSessionCache;

    private DeliverService deliverService;

    private ServiceProviderService serviceProviderService;

    @Autowired
    public void setDeliverService(DeliverService deliverService) {
        this.deliverService = deliverService;
    }

    @Autowired
    public void setServiceProviderService(ServiceProviderService serviceProviderService) {
        this.serviceProviderService = serviceProviderService;
    }

    @Autowired
    public void setServiceProviderSessionCache(ServiceProviderSessionCache serviceProviderSessionCache) {
        this.serviceProviderSessionCache = serviceProviderSessionCache;
    }

    /**
     * 查询需要送货的订单。
     *
     * @param pageParam   分页参数。
     * @param orderStatus 订单状态。
     * @return 订单数据。
     */
    @ResponseBody
    @RequestMapping(value = "/queryOrders", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage queryOrders(PageParam pageParam, @RequestParam("orderStatus") Integer orderStatus,
                                     @RequestHeader(AppConfig.TOKEN_KEY) String token) {
        logger.info("配送控制器：查询订单：订单状态：" + orderStatus);

        ServiceProvider serviceProvider = serviceProviderSessionCache.get(token);
        if (serviceProvider == null || !StringUtils.hasText(serviceProvider.getStationId()))
            return ResultMessage.newFailure("当前服务人员无效！");
        PageData pageData = deliverService.queryOrders(pageParam, serviceProvider.getServiceProviderId(), serviceProvider.getStationId(), orderStatus);
        return ResultMessage.newSuccess().setData(pageData);

    }

    /**
     * 配送员抢单。
     *
     * @param orderId 订单ID。
     * @return 订单数据。
     */
    @ResponseBody
    @RequestMapping(value = "/acceptOrder", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage acceptOrder(String orderId, @RequestHeader(AppConfig.TOKEN_KEY) String token) {
        logger.info("配送控制器：配送员抢单：订单ID：" + orderId);

        ServiceProvider serviceProvider = serviceProviderSessionCache.get(token);
        if (!StringUtils.hasText(orderId))
            return ResultMessage.newFailure("订单ID不能为空！");
        deliverService.acceptOrder(serviceProvider, orderId);
        return ResultMessage.newSuccess();

    }

    /**
     * 配送员同意取消定单。
     *
     * @param orderId 订单ID。
     * @return 订单数据。
     */
    @ResponseBody
    @RequestMapping(value = "/agreeCancelOrder", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage agreeCancelOrder(String orderId, @RequestHeader(AppConfig.TOKEN_KEY) String token) {
        logger.info("配送控制器：配送员同意取消定单：token：" + token + " 订单ID：" + orderId);
        ServiceProvider serviceProvider = serviceProviderSessionCache.get(token);
        deliverService.agreeCancelOrder(serviceProvider, orderId);
        return ResultMessage.newSuccess();
    }

    /**
     * 配送员拒绝取消定单。
     *
     * @param orderId 订单ID。
     * @return 订单数据。
     */
    @ResponseBody
    @RequestMapping(value = "/refuseCancelOrder", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage refuseCancelOrder(String orderId, String reason, @RequestHeader(AppConfig.TOKEN_KEY) String token) {
        logger.info("配送控制器：配送员拒绝取消定单：token：" + token + " 订单ID：" + orderId);
        ServiceProvider serviceProvider = serviceProviderSessionCache.get(token);
        deliverService.refuseCancelOrder(serviceProvider, orderId, reason);
        return ResultMessage.newSuccess();
    }

    /**
     * 服务员对订单取货。
     *
     * @param orderId 订单ID。
     * @return 订单数据。
     */
    @ResponseBody
    @RequestMapping(value = "/getOrderGoods", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage getOrderGoods(String orderId, @RequestHeader(AppConfig.TOKEN_KEY) String token) {
        logger.info("配送控制器：从订单取货：订单ID：" + orderId);
        ServiceProvider serviceProvider = serviceProviderSessionCache.get(token);
        if (!StringUtils.hasText(orderId))
            return ResultMessage.newFailure("订单ID不能为空！");
        deliverService.getOrderGoods(serviceProvider, orderId);
        return ResultMessage.newSuccess();
    }

    /**
     * 服务员通知用户取货。
     *
     * @param orderId 订单ID。
     * @return 订单数据。
     */
    @ResponseBody
    @RequestMapping(value = "/noticeUserGet", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage noticeUserGet(String orderId, @RequestHeader(AppConfig.TOKEN_KEY) String token) {
        logger.info("配送控制器：通知用户取货：订单ID：" + orderId);

        ServiceProvider serviceProvider = serviceProviderSessionCache.get(token);
        if (!StringUtils.hasText(orderId))
            return ResultMessage.newFailure("订单ID不能为空！");
        deliverService.noticeUserGet(serviceProvider, orderId);
        return ResultMessage.newSuccess();

    }

    /**
     * 服务员将商品已送达。
     *
     * @param orderId 订单ID。
     * @return 订单数据。
     */
    @ResponseBody
    @RequestMapping(value = "/deliveredGoods", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage deliveredGoods(String orderId, String receiveCode, @RequestHeader(AppConfig.TOKEN_KEY) String token) {
        logger.info("配送控制器：商品送达客户：订单ID：" + orderId);

        ServiceProvider serviceProvider = serviceProviderSessionCache.get(token);
        if (!StringUtils.hasText(orderId))
            return ResultMessage.newFailure("订单ID不能为空！");
        deliverService.deliveredGoods(serviceProvider, orderId, receiveCode);
        return ResultMessage.newSuccess();

    }

    /**
     * 统计今天完成的订单。
     *
     * @return 订单数据。
     */
    @ResponseBody
    @RequestMapping(value = "/countOrdersToday", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage countOrdersToday(@RequestHeader(AppConfig.TOKEN_KEY) String token) {
        logger.info("配送控制器：统计今天完成的订单");

        ServiceProvider serviceProvider = serviceProviderSessionCache.get(token);
        CountOrdersVO countOrdersVO = deliverService.countOrdersToday(serviceProvider.getServiceProviderId());
        serviceProvider = serviceProviderService.findByServiceProviderId(serviceProvider.getServiceProviderId());
        if (serviceProvider != null) {
            countOrdersVO.setStatus(serviceProvider.getStatus());
            countOrdersVO.setServiceProviderId(serviceProvider.getServiceProviderId());
        }
        return ResultMessage.newSuccess().setData(countOrdersVO);

    }

    /**
     * 查询今天已完成的订单。
     */
    @ResponseBody
    @RequestMapping(value = "/queryCompleteOrdersToday", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage queryCompleteOrdersToday(@RequestHeader(AppConfig.TOKEN_KEY) String token) {
        logger.info("配送控制器：查询需要已完成的订单");

        ServiceProvider serviceProvider = serviceProviderSessionCache.get(token);
        List<DeliverOrderVO> orderVOs = deliverService.queryCompleteOrdersToday(serviceProvider);
        return ResultMessage.newSuccess().setData(orderVOs);

    }

    /**
     * 查询今天被取消的订单。
     */
    @ResponseBody
    @RequestMapping(value = "/queryCancelOrdersToday", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage queryCancelOrdersToday(@RequestHeader(AppConfig.TOKEN_KEY) String token) {
        logger.info("配送控制器：查询需要被取消的订单");

        ServiceProvider serviceProvider = serviceProviderSessionCache.get(token);
        List<DeliverOrderVO> orderVOs = deliverService.queryCancelOrdersToday(serviceProvider);
        return ResultMessage.newSuccess().setData(orderVOs);

    }

}
